<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index()
    {        
        $q = addslashes(_GET('q'));
        $cate_id = _GET('cate_id', 0);
        $lang = _GET('lang', 0);
        // $search_by = _GET('search_by', 0);
        $tags = _GET('ids');
        $tags = array_filter(explode(',', $tags));
        
        $order = _GET('order');
        $order_key = _GET('order_key');
        $order_key_check = array('user_id');
        $order_check = array('asc', 'desc');
        if (!in_array($order, $order_check)) {
            $order = 'desc';
        }
        if (!in_array($order_key, $order_key_check)) {
            $order = 'add_time';
        }
        $order = $order_key.' '.$order;
        if (!empty($q)) {
            $like ="AND title like '%$q%'";
        } else {
            $like = '';
        }
        $like .= " AND language=$lang ";

        $data = _model('assets')->getList(" WHERE cate_group_id=$cate_id $like ORDER By $order ", new Pager(20));

        $this->view->assign($data);
        $this->view->display('admin/manage_asset.html');
    }
    
    function add()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $info = _POST('info', array());
            $asset_id = _POST('id', 0);
            $save_continue = _POST('save_continue');
            $info['user_id'] = $this->info['id'];
            $info['add_time'] = time();
            

            
            if (empty($info['title'])) {
                $this->msg('title is empty');
            }

            if ($asset_id) { // ID存在为编辑否则为添加
                _model('assets')->update(array('id'=>$asset_id), $info);
            } else {
                $id = _model('assets')->create($info);
            }
            
            $this->cache->ns('assets')->delete();
            
            if ($info['cate_group_id']=='9') {
                $this->redirect();
            } else {
            $this->msg('Operation was successful','admin/asset_manage.html?cate_id='.$info['cate_group_id'].'&lang='.$info['language']);
            }

 
            
        } else {
            $this->view->display('admin/add_asset.html');
        }
    }
    
    function edit()
    {
        $id = _GET('id', 0);
        if ($id) {
            $data = _model('assets')->read(array('id'=>$id));
            $tags = $this->get_tags();
            $assets_tag = _model('assets_tag')->getList(array('asset_id'=>$id));
            $temp = array();
            if (!empty($assets_tag)) {
                foreach ($assets_tag as $key => $value) {
                    $temp[] = $value['tag_id'];
                }
            }
            $data['tags'] = array_unique(array_filter(explode(',', $data['tags'])));
            $data['tags'] = implode(',', $data['tags']);
            $data['append'] = unserialize($data['append']);
            $this->view->assign('data', $data);
            $this->view->assign('assets_tag', $temp);
            $this->view->assign('tags', $tags);
            $this->view->display('admin/add_asset.html');
        }
    }
    
    function delete()
    {
        $id = _GET('id', 0);
        if ($id) {
            
            $this->cache->ns('assets')->delete();
            
            _model('assets')->delete(array('id'=>$id));
            $this->redirect();
        }
    }
    
    function assign_asset()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $asset_id = _POST('asset_id', 0);
            $select = _POST('select', 0);
            if (!$asset_id && !$select) {
                $this->msg('Lacking parameter');
            }
            _model('assets')->update(array('id'=>$asset_id), array('user_id'=>$select));
            
            $user = _model('user')->getInfoById($select);
            $asset = _model('assets')->read(array('id'=>$asset_id));
            $this->view->assign('asset_title', $asset['title']);
            $this->view->assign('link_front', SITE_URL."/asset/{$asset_id}.html");
            $this->view->assign('link_admin', SITE_URL."/admin/asset_manage/view.html?id={$asset_id}");
            $content = $this->view->fetch('lib/mail_asset_ assign.html');
            $this->send_email($user['email'], $asset['title'].' has been assigned to you in ISA', $content);

            $this->cache->ns('assets')->delete();
            
            $this->msg('Operation was successful', 'admin/asset_manage.html');
        } else {
            $id = _GET('id', 0);
            $asset =  _model('assets')->read(array('id'=>$id));
            if (!$asset) {
                $this->msg('not content');
            }
            $data = _model('user')->getList('WHERE user_group > 0 ORDER BY add_time DESC', new Pager(20));
            $user_group = $this->get_admin_group();
            $this->view->assign('info', $asset);
            $this->view->assign($data);
            $this->view->assign('user_group', $user_group);
            $this->view->display('admin/assign_asset.html');
        }
    }
    
    function view()
    {
       $id = _GET('id', 0);
       if (!$id || !$info = _model('assets')->get_info($id)) {
            $this->msg('Lacking parameter');
       }
       $info['tags'] = implode(",", $info['tags']);
       $temp_r = array();
       foreach ($info['set_tags'] as $key => $value) {
            $temp_r[$value['tag_name']] = array();
            foreach ($value['tags'] as $k => $v) {
                $temp_r[$value['tag_name']][] = $v['tag_name'];
            }
       }
       foreach ($temp_r as $key => $value) {
            $temp_r[$key] = implode(', ', $value);
       }
       
       //获取Related
       $related = _model('assets_relate')->getList(array('asset_id'=>$id));
       $this->view->assign('related', $related);
       
       
       $assets_version = _model('assets_version')->getList('WHERE asset_id='.$id.' ORDER BY view_order ASC');
       $this->view->assign('assets_version', $assets_version);
       $this->view->assign('tags', $temp_r);
       $this->view->assign('info', $info);
       $this->view->display('admin/view_asset.html');
    }
    
    function ajax_add_version()
    {
        $version_name = _GET('name');
        $asset_id = intval(_GET('asset_id', 0));
        if (!$asset_id || !$version_name) {
            die('0');
        } else {
            $time = time();
            $id = _model('assets_version')->create(array('asset_id'=>$asset_id,'version_name'=>$version_name,'add_time'=>$time,'update_time'=>$time));
            if ($id) {
                $info = array('id'=>$id, 'version_name'=>$version_name, 'add_time'=>date('m/d/Y', $time), 'files_num'=>0, 'id'=>$id);
                die(json_encode($info));
            } else {
                die('0');
            }
        }
    }
    
    function ajax_edit_version()
    {
        $version_name = _GET('name');
        $id = intval(_GET('id', 0));
        if (!$version_name || !$id) {
            die('NULL');
        }
        _model('assets_version')->update(array('id'=>$id), array('version_name'=>$version_name));
        die($version_name);
    }
    
    function ajax_delete_version()
    {
        $id = intval(_GET('id', 0));
        if (!$id) {
            die('NULL');
        }
        _model('assets_version')->delete(array('id'=>$id));
        die('1');
    }
    
    function add_file()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $file_info = _POST('add_file', array());
            if (!$file_info['file_name'] || !$file_info['version_id'] || !$file_info['file_path'] || !$file_info['file_size'] || !$file_info['language'] || !$file_info['ext']) {
                $this->msg('Lacking parameter');
            }

            $v_info = _model('assets_version')->read(array('id'=>$file_info['version_id']));
            $file_info['asset_id'] = $v_info['asset_id'];
            
            //将语言TAG到ASSET的TAG中
            $tag_info = _model('tags')->read(array('id'=>$file_info['language']));
            $asset_tag = _model('assets_tag')->read(array('asset_id'=>$file_info['asset_id'], 'tag_id'=>$file_info['language']));
            if (empty($asset_tag)) {
                _model('assets_tag')->create(array('asset_id'=>$file_info['asset_id'], 'tag_id'=>$file_info['language']));
            }
            $file_info['language'] = $tag_info['tag_name'];
            //
            
            $file_info['add_time'] = time();
            $id = _model('assets_file')->create($file_info);
            if ($id) {
                $count = _model('assets_file')->getCount(array('version_id'=>$file_info['version_id']));
                _model('assets_version')->update(array('id'=>$file_info['version_id']), array('files_num'=>$count, 'update_time'=>time()));
                $this->redirect();
            }
        } else {
            $id = _GET('id', 0);
            $v_info = _model('assets_version')->read(array('id'=>$id));
            if (!$id || !$v_info) {
                $this->msg('Lacking parameter');
            }
            $files = _model('assets_file')->getList(array('version_id'=>$id));

            $this->view->assign('files', $files);
            $this->view->assign('file_language', _model('tags')->getList("WHERE parent_id = (SELECT id FROM tags WHERE is_system = '1')"));
            $this->view->assign('v_info', $v_info);
            $this->view->display('admin/add_file.html');
        }
    }
    
    function update_file()
    {
        $info = _POST('up_file', array());
        $v_id = _POST('v_id', 0);
        foreach ($info as $key => $val) {
            if (intval($key)) {
                _model('assets_file')->update(array('id'=>$key), array('file_name'=>$val['file_name'], 'language'=>$val['language']));
            }
        }
        $this->msg('Operation was successful', 'admin/asset_manage/add_file.html?id='.$v_id);
    }
    
    function delete_file()
    {
        $id = _GET('id', 0);
        if (!$id) {
            $this->msg('Lacking parameter');
        }
        $file_info = _model('assets_file')->read(array('id'=>$id));
        _model('assets_file')->delete(array('id'=>$id));
        $count = _model('assets_file')->getCount(array('version_id'=>$file_info['version_id']));
        _model('assets_version')->update(array('id'=>$file_info['version_id']), array('files_num'=>$count, 'update_time'=>time()));
        $this->redirect();
    }
    
    function add_related_asset()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $relate_asset_id = _POST('relate_asset_id', array());
            $asset_id = _POST('asset_id', 0);
            
            if (!$asset_id) {
                $this->msg('Lacking parameter');
            }
            //处理
            $back_post = _POST('back_post');
            if (!empty($back_post)) {
                $relate_asset_id = array_merge($relate_asset_id, explode('|', $back_post));
            }
            //
            _model('assets_relate')->delete(array('asset_id'=>$asset_id));
            foreach ($relate_asset_id as $value) {
                _model('assets_relate')->create(array('asset_id'=>$asset_id, 'relate_asset_id'=>$value, 'add_time'=>time()));
            }
            $this->redirect();
        } else {
            $asset_id = _GET('id', 0);
            $search_by = _GET('search_by', 0);
            
            $q = addslashes(_GET('q'));
            
            $order = _GET('order');
            $order_key = _GET('order_key');
            $order_key_check = array('user_id');
            $order_check = array('asc', 'desc');
            if (!in_array($order, $order_check)) {
                $order = 'desc';
            }
            if (!in_array($order_key, $order_key_check)) {
                $order = 'add_time';
            }
            $order = $order_key.' '.$order;
            
            if (!$asset_id || !$asset_info = _model('assets')->read(array('id'=>$asset_id))) {
                $this->msg('Lacking parameter');
            }
            if (!$q) {
                $data = _model('assets')->getList(" ORDER BY $order", new Pager(20));
            } else {
                if ($search_by) {
                    $data = _model('assets')->getList("WHERE user_id=(SELECT id FROM user WHERE full_name='$q') ORDER BY $order", new Pager(20));
                } else {
                    $data = _model('assets')->getList("WHERE title LIKE '%{$q}%' ORDER BY $order", new Pager(20));
                }
            }
            
            $relate_assets =  _model('assets_relate')->getList("WHERE asset_id = $asset_id");
            $check_assets = array();
            foreach ($relate_assets as $key => $value) {
                $check_assets[] = $value['relate_asset_id'];
            }
            //处理
            $back_post = $check_assets;
            foreach ($data['data'] as $key => $value) {
                if (in_array($value['id'], $check_assets)) {
                    $k = array_search($value['id'], $check_assets);
                    unset($back_post[$k]);
                }
            }
            $back_post = implode('|', $back_post);
            //
            
            
            $this->view->assign($data);
            $tags = $this->get_tags();
            $this->view->assign('tags', $tags);
            $this->view->assign('check_assets', $check_assets);
            $this->view->assign('back_post', $back_post);
            $this->view->assign('asset_id', $asset_id);
            $this->view->display('admin/add_related_asset.html');
        }
    }
    
    function ajax_delete_related()
    {
       $id = _GET('id', 0);
       if ($id) {
          _model('assets_relate')->delete(array('id'=>$id));
          die('1');
       } else {
         die('0');
       }
    }
    
    function update_view_order_version()
    {
        $ids = _POST('ids', array());
        if (empty($ids)) {
            return '';
        }
        foreach ($ids as $key => $value) {
            _model('assets_version')->update(array('id'=>$value), array('view_order'=>$key));
        }
    }
}


?>